<?php defined("MAIN_DIR") or die("No direct access");

$city_id = UV::readInt($route->getPath(5));

$error = array();
if (isset($_POST['action']) && $_POST['action'] == "edit")
{
	$path = trim(preg_replace("/'/","",$_POST['path']));
	$path = preg_replace("/\"/","",$path);
	$power = $_POST['power'];
	$city_name = trim(filter_var($_POST['city_name'],FILTER_SANITIZE_MAGIC_QUOTES));
	$city_name_rod = trim(filter_var($_POST['city_name_rod'],FILTER_SANITIZE_MAGIC_QUOTES));
	$city_name_pred = trim(filter_var($_POST['city_name_pred'],FILTER_SANITIZE_MAGIC_QUOTES));
	
	$edit_query = "update `city` set `path`='$path',`power`='$power',`city_name`='$city_name',`city_name_rod`='$city_name_rod',`city_name_pred`='$city_name_pred' where `city_id` = '$city_id' limit 1";
	if (!mysql_query($edit_query))
		$error['query'] = "Не удалось отредактировать";
}
if ($city_id)
{
	$city_res = mysql_query("select * from `city` where `city_id` = '$city_id' limit 1");
	
	if (mysql_num_rows($city_res) == 0) {
		to("/admin/site/cities/");
	}
	
	$city_row = mysql_fetch_array($city_res);
}

require_once ADMIN.HEADFILE;
?>
<div align="left">
<h1>Редактирование города</h1>
<hr /><?php
if (isset($_POST['action']) && $_POST['action'] == "edit" && count($error) == 0)
	echo "<p style='color:green'>Успешно отредактировано</p>";
elseif (count($error) > 0)
	echo "<p style='color:red'>".implode("<br />",$error)."</p>";
?><form method='POST'>
<input type='hidden' name='action' value='edit' />
<input type='hidden' name='city_id' value='<?php echo $city_row['city_id']; ?>' />
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td width="200px">ID</td>
		<td width="300px"><input style="width:200px" type='text' name='city_id' value='<?php echo $city_row['city_id'];?>' disabled /></td>
	</tr>
	<tr>
		<td>Путь</td>
		<td><input style='width:100%' type='text' name='path' value='<?php echo $city_row['path'];?>' /></td>
	</tr>
	<tr>
		<td>Включен</td>
		<td>
			<input type='radio' name='power' value='on' <?php if ($city_row['power'] != "off") echo "checked='checked'"; ?> /> Да
			<input type='radio' name='power' value='off' <?php if ($city_row['power'] == "off") echo "checked='checked'"; ?> /> Нет
		</td>
	</tr>
	<tr>
		<td>Название города</td>
		<td><input style='width:100%' type='text' name='city_name' value='<?php echo $city_row['city_name'];?>' /></td>
	</tr>
	<tr>
		<td>Название в род. падеже</td>
		<td><input style='width:100%' type='text' name='city_name_rod' value='<?php echo $city_row['city_name_rod'];?>' /></td>
	</tr>
	<tr>
		<td>Название в пред. падеже</td>
		<td><input style='width:100%' type='text' name='city_name_pred' value='<?php echo $city_row['city_name_pred'];?>' /></td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Сохранить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>